<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('order_sn')->unique()->comment('订单编号');
            $table->string('payment_sn')->nullable()->comment('支付编号（第三方）');
            $table->unsignedInteger('coupon_id')->default(0)->comment('优惠券ID');
            $table->json('logistics_code')->nullable()->comment('物流编码信息');
            $table->unsignedBigInteger('user_id')->comment('用户ID');
            $table->foreign('user_id')->references('id')->on('wx_members')->onDelete('cascade');
            $table->json('address')->comment('收货地址（json字符串）');
            $table->decimal('freight_price',10,2)->default(0)->comment('订单运费');
            $table->decimal('total_amount', 10, 2)->comment('订单总金额');
            $table->string('remark',500)->nullable()->comment('订单备注');
            $table->unsignedTinyInteger('payment_method')->default(1)->nullable()->comment('支付方式 1 微信 2支付宝 默认微信');
            $table->string('ship_status')->default(\App\Models\Order::SHIP_STATUS_AWAITING_PAYMENT)->comment('订单状态');
            $table->text('extra')->nullable()->comment('非核心字段冗余');
            $table->boolean('closed')->default(false)->comment('是否关闭订单');
            $table->dateTime('reviewed_at')->nullable()->comment('评论时间');
            $table->dateTime('paid_at')->nullable()->comment('支付时间');
            $table->dateTime('cancel_delete')->nullable()->comment('用户删除时间');
            $table->dateTime('confirmed_at')->nullable()->comment('确认收货时间');
            $table->index('order_sn');
            $table->index('user_id');
            $table->timestamps();
        });
        \DB::statement("ALTER TABLE `hu_orders` comment '订单表'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('orders');
    }
}
